package com.javalong.affairs.base.ui;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
 * Created by javalong on 2015/12/20 0020.
 */
public class MyItemDecoration extends RecyclerView.ItemDecoration {

    enum Orientation {
        Vertical,
        Horizontal
    }

    private Context mContext;
    private Orientation mOrientation;
    private Drawable mDivier;
    private int mDividerSize = 1;

    public MyItemDecoration(Context context) {
        mContext = context;
        mOrientation = Orientation.Vertical;
        setDefaultDivier();
    }

    public MyItemDecoration(Context context, Orientation orientation) {
        mContext = context;
        mOrientation = orientation;
        setDefaultDivier();
    }

    //默认的 divider 就用在 listDivier里面设置的
    private void setDefaultDivier() {
        TypedArray typedArray = mContext.obtainStyledAttributes(new int[]{android.R.attr.listDivider});
        int resId = typedArray.getResourceId(0, android.R.attr.listDivider);
        if (resId != 0) {
            mDivier = typedArray.getResources().getDrawable(resId);
        } else {
            mDivier = typedArray.getDrawable(android.R.attr.listDivider);
        }
        typedArray.recycle();
    }

    //向外提供接口，可以直接注入
    public void setDivier(Drawable divier) {
        this.mDivier = divier;
    }

    public void setDividerSize(int dividerSize) {
        this.mDividerSize = dividerSize;
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);
        //开始 画线
        //可以在这里判断 是画水平的线 还是 画垂直的线
        switch (mOrientation) {
            case Vertical:
                drawVerticalLine(c, parent);
                break;
            case Horizontal:
                drawHorizontalLine(c, parent);
                break;
        }
    }

    private void drawHorizontalLine(Canvas c, RecyclerView parent) {
        int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View childView = parent.getChildAt(i);
            int left = childView.getLeft() + childView.getPaddingLeft();
            int right = left + mDividerSize;
            int top = childView.getTop() - childView.getPaddingTop();
            int bottom = childView.getBottom() - childView.getPaddingBottom();
            mDivier.setBounds(left, right, top, bottom);
            mDivier.draw(c);
        }
    }

    private void drawVerticalLine(Canvas c, RecyclerView parent) {
        int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View childView = parent.getChildAt(i);
            int left = childView.getLeft() - childView.getPaddingLeft();
            int right = childView.getRight() - childView.getPaddingRight();
            int top = childView.getBottom() + childView.getPaddingBottom();
            int bottom = top + mDividerSize;
            mDivier.setBounds(left, top, right, bottom);
            mDivier.draw(c);
        }
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        switch (mOrientation) {
            case Vertical:
                outRect.set(0, 0, 0, mDividerSize);
                break;
            case Horizontal:
                outRect.set(0, 0, mDividerSize, 0);
                break;
        }
    }
}
